package cn.bistu.cs.zszproject1;

import java.util.*;

public class Calculate {
    private StringBuilder pending;
    private String[] arrayList;
    private Map<String,Integer> operators = new HashMap<String, Integer>();
    public Calculate(StringBuilder p){
        pending = p;
        arrayList = p.toString().split(" ");
        operators.put("-", 1);
        operators.put("+", 1);
        operators.put("*", 2);
        operators.put("/", 2);
        operators.put("(", 0);
        operators.put("!", 3);

    }

    //后缀表达式计算
    public String compute(){

        Deque<String> deque = new LinkedList<>();//栈
        String s;
        for(int i=0;i<arrayList.length;i++){
            s=arrayList[i];
            if(s.matches("-?[0-9]+.?[0-9]*")){                      //是数字
                deque.addFirst(s);
            }else if(operators.containsKey(s)){
                if(s.equals("!")){
                    int n = Integer.parseInt(deque.removeFirst());
                    int res = 1;
                    while(n>0){
                        res *= n--;
                    }
                    deque.addFirst(""+res);
                }else{
                    Double B=Double.parseDouble(deque.removeFirst());
                    Double A=Double.parseDouble(deque.removeFirst());
                    switch (s.charAt(0)){
                        case '+':
                            A += B;deque.addFirst("" + A);break;
                        case '-':
                            A -= B;deque.addFirst("" + A);break;
                        case '*':
                            A *= B;deque.addFirst("" + A);break;
                        case  '/':
                            A /= B;deque.addFirst("" + A);break;
                    }

                }
            }
        }
        return deque.size()==1?deque.removeFirst():"Compute Error!";
    }



    public StringBuilder infixTOSuffix(){
        //查看arraylist是否正确
//        for(String a:arrayList){
//            System.out.println(a);
//        }
//        System.out.println(pending.length());
        StringBuilder res = new StringBuilder("");
        Deque<String> deque = new LinkedList<String>();//栈
        String s = null;
        for(int i=0;i<arrayList.length;i++){
            s=arrayList[i];
            if(s.matches("-?[0-9]+.?[0-9]*")){
                res.append(s+",");
            }
            else if (operators.containsKey(s)||s.equals(")")) {
                if(deque.isEmpty()){
                    deque.addFirst(s);
                }else if(s.equals(")")){
                    while(!deque.peek().equals("(")){
                        res.append(deque.removeFirst()+",");
                    }
                    deque.removeFirst();
                } else if(operators.get(deque.peek())<operators.get(s)||s.equals("(")){
                    deque.addFirst(s);
                }else {
                    while(!deque.isEmpty()&&operators.get(deque.peek())>=operators.get(s)){
                        res.append(deque.removeFirst()+",");
                    }
                    deque.addFirst(s);
                }
            }
        }
        while(deque.size()!=1){
            res.append(deque.removeFirst()+",");
        }
        res.append(deque.removeFirst());
        arrayList = res.toString().split(",");
        return res;
    }

}


